package com.spikything.gfx {
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.PixelSnapping;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class Scale9Bitmap extends Sprite {
		
		private var tlBitmap:Bitmap;
		private var tBitmap:Bitmap;
		private var trBitmap:Bitmap;
		
		private var mlBitmap:Bitmap;
		private var mBitmap:Bitmap;
		private var mrBitmap:Bitmap;
		
		private var blBitmap:Bitmap;
		private var bBitmap:Bitmap;
		private var brBitmap:Bitmap;
		
		public var rect:Rectangle;
		
		public function Scale9Bitmap(bd:BitmapData, scale9:Rectangle) {
			rect = scale9;
			
			var tlRect:Rectangle = new Rectangle(0, 0, scale9.left, scale9.top);
			tlBitmap = new Bitmap(getBitmapSubsection(bd, tlRect), PixelSnapping.ALWAYS);
			var tRect:Rectangle = new Rectangle(scale9.left, 0, scale9.width, scale9.top);
			tBitmap = new Bitmap(getBitmapSubsection(bd, tRect), PixelSnapping.ALWAYS);
			var trRect:Rectangle = new Rectangle(scale9.right, 0, bd.width-scale9.right, scale9.top);
			trBitmap = new Bitmap(getBitmapSubsection(bd, trRect), PixelSnapping.ALWAYS);
			
			var mlRect:Rectangle = new Rectangle(0, scale9.top, scale9.left, scale9.height);
			mlBitmap = new Bitmap(getBitmapSubsection(bd, mlRect), PixelSnapping.ALWAYS);
			var mRect:Rectangle = new Rectangle(scale9.left, scale9.top, scale9.width, scale9.height);
			mBitmap = new Bitmap(getBitmapSubsection(bd, mRect), PixelSnapping.ALWAYS);
			var mrRect:Rectangle = new Rectangle(scale9.right, scale9.top, bd.width-scale9.right, scale9.height);
			mrBitmap = new Bitmap(getBitmapSubsection(bd, mrRect), PixelSnapping.ALWAYS);
			
			var blRect:Rectangle = new Rectangle(0, scale9.bottom, scale9.left, bd.height-scale9.bottom);
			blBitmap = new Bitmap(getBitmapSubsection(bd, blRect), PixelSnapping.ALWAYS);
			var bRect:Rectangle = new Rectangle(scale9.left, scale9.bottom, scale9.width, bd.height-scale9.bottom);
			bBitmap = new Bitmap(getBitmapSubsection(bd, bRect), PixelSnapping.ALWAYS);
			var brRect:Rectangle = new Rectangle(scale9.right, scale9.bottom, bd.width-scale9.right, bd.height-scale9.bottom);
			brBitmap = new Bitmap(getBitmapSubsection(bd, brRect), PixelSnapping.ALWAYS);
			
			addChild(tlBitmap);
			addChild(tBitmap);
			addChild(trBitmap);
			
			addChild(mlBitmap);
			addChild(mBitmap);
			addChild(mrBitmap);
			
			addChild(blBitmap);
			addChild(bBitmap);
			addChild(brBitmap);
			
			resize(bd.width, bd.height);
		}
		
		override public function set width(w:Number):void {
			resize(w, height)
		}
		override public function set height(h:Number):void {
			resize(width, h)
		}
		//override public function set scaleX(sx:Number):void {
			//super.scaleX = sx;
			//resize();
		//}
		//override public function set scaleY(sy:Number):void {
			//super.scaleY = sy;
			//resize();
		//}

		protected function resize(w:Number, h:Number):void {
			brBitmap.x = Math.round(w-brBitmap.width);
			brBitmap.y = Math.round(h-brBitmap.height);
			
			trBitmap.x = brBitmap.x;
			
			blBitmap.y = brBitmap.y;
			blBitmap.width = tlBitmap.width;
			blBitmap.height = brBitmap.height;
			
			tBitmap.x = tlBitmap.x+tlBitmap.width;
			tBitmap.width = trBitmap.x - tlBitmap.width;
			tBitmap.height = tlBitmap.height;
			
			mlBitmap.y = tlBitmap.height;
			mlBitmap.width = tlBitmap.width;
			mlBitmap.height = brBitmap.y - tlBitmap.height;
			
			mrBitmap.x = brBitmap.x;
			mrBitmap.y = tlBitmap.height;
			mrBitmap.width = brBitmap.width;
			mrBitmap.height = brBitmap.y - tlBitmap.height;
			
			mBitmap.x = tlBitmap.width;
			mBitmap.y = tlBitmap.height;
			mBitmap.width = brBitmap.x - tlBitmap.width;
			mBitmap.height = brBitmap.y - tlBitmap.height;
			
			bBitmap.x = tlBitmap.width;
			bBitmap.y = brBitmap.y;
			bBitmap.width = brBitmap.x - tlBitmap.width;
			bBitmap.height = brBitmap.height;
		}
		
		public static function getBitmapSubsection(bd:BitmapData, rect:Rectangle):BitmapData {
			var ret:BitmapData = new BitmapData(rect.width, rect.height);
			ret.copyPixels(bd, rect, new Point());
			return ret;
		}
		
	}
	
}